<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Attendence_emp extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct()
    {
        parent::__construct();
        $this->load->model('attendence_model');
        $this->load->model('student_model');
        if(!$this->session->userdata('is_logged_in')){
            redirect('admin/login');
        }
    }
 
    
public function index($isPdf=false)
    {

    	date_default_timezone_set('asia/dhaka'); //Asia/Dhaka
    	$today=date("d/m/Y");
    	
        //all the posts sent by the view
    	$start_date=$this->input->post('start_date');
    	$end_date=$this->input->post('end_date');
        $dept_id=$this->input->post('dept_id'); 
        $search_string = $this->input->post('search_string');        
        $order = $this->input->post('order'); 
        
        //pagination settings
        $config['per_page'] = 50;
        $config['base_url'] = base_url().'index.php/admin/reports/attendence_emp';
        $config['use_page_numbers'] = TRUE;
        $config['num_links'] = 20;
        $config['full_tag_open'] = '<ul>';
        $config['full_tag_close'] = '</ul>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a>';
        $config['cur_tag_close'] = '</a></li>';
        //initializate the panination helper 
        //$this->pagination->initialize($config);   
        
	//limit end
        $page = $this->uri->segment(3);

        //math to get the initial record to be select in the database
        $limit_start = ($page * $config['per_page']) - $config['per_page'];
        if ($limit_start < 0){
            $limit_start = 0;
        } 
        $limit_end=$limit_start+ $config['per_page'];
        
        $order_type = 'Asc';
        $filter_session_data['order_type'] = $order_type;
          
        if($start_date==null || $end_date==null)
        {
        	$start_date=$today;
        	$end_date=$today;
        }
        

        	if($start_date!=null){
        		$filter_session_data['dept_id_selected'] = $start_date;
        	}else{
        		$start_date = $this->session->userdata('start_date_selected');
        		$filter_session_data['dept_id_selected'] = $start_date;
        	}
        	$data['start_date_selected'] = $start_date;
        	
        	
        	
        	if($end_date!=null){
        		$filter_session_data['dept_id_selected'] = $end_date;
        	}else{
        		$end_date = $this->session->userdata('end_date_selected');
        		$filter_session_data['dept_id_selected'] = $end_date;
        	}
        	$data['end_date_selected'] = $end_date;
        	
        	
        	
            if($dept_id!=null){
                $filter_session_data['dept_id_selected'] = $dept_id;
            }else{
                $dept_id = $this->session->userdata('dept_id_selected');
                $filter_session_data['dept_id_selected'] = $dept_id;
            }
            $data['dept_id_selected'] = $dept_id;
            
            if( $order!=null){
            	$filter_session_data['order'] =  $order;
            }else{
            	$order = $this->session->userdata('order');
            	$filter_session_data['order'] =  $order;
            }
            $data['order']  =  $order;
            
            

            if($search_string!=null){
                $filter_session_data['search_string_selected'] = $search_string;
            }else{
                $search_string = $this->session->userdata('search_string_selected');
                $filter_session_data['search_string_selected'] = $search_string;
            }
            
            $data['search_string_selected'] = $search_string;
            
            //save session data into the session
            $this->session->set_userdata($filter_session_data);

            //fetch student data into arrays
            //$data['sessions'] = $this->student_model->get_session_ids();

            $data['count_employees']= $this->attendence_model->count_emp_attendence($dept_id, $start_date, $end_date, $search_string, $order, $limit_start, $limit_end);
            $config['total_rows'] = $data['count_employees'];
            $data['employees'] = $this->attendence_model->get_emp_attendence($dept_id, $start_date, $end_date, $search_string, $order, $limit_start, $limit_end);
            
            if($isPdf)return $data;
            
            $this->pagination->initialize($config);
            //load the view
            $data['main_content'] = 'admin/attendence_emp_view';
            $this->load->view('includes/template2', $data);  

    }//index
   ////////////////////////////////////////////
   function emp_attendence_report()
   {
   	 $isPdf=true;
   	 $data=$this->index($isPdf);

   	 ini_set('memory_limit','1024M');
   	 ini_set('max_execution_time', 900);
   	 $htmlHeader="<html>
						<head>
						<style>
						@page {
							margin: 50px 50px 20px 50px;
						}
						#table_content{
   	 
                        }
						#footer {
							position: fixed;
							bottom: -50px;
							height: 30px;
							background-color: #333399;
						}
						#header {
							position: fixed;
							top: -115px;
							width: 100%;
							height: 106px;
							background: url('http://localhost/CodeIgniter_214/resourse/images/header_left.jpg') no-repeat;
						}
						/* table css */
						table.gridtable {
						  font-family:'Times New Roman', Times, serif;
						  font-size:14px;
						  text-align:center;
						  border-collapse: collapse;
						  width: 690px;
						}
						table.gridtable th {
						  padding: 0.25rem;
						  text-align: center;
						  border: 1px solid #000;
						}
   	 
						table.gridtable td {
   	 		              font-size:16px;
						  padding: 0.25rem;
						  text-align: center;
						  border: 1px solid #000;
						}
						#tablePageHeader
						{
						 font-size:24px;
						 font-family:'Times New Roman', Times, serif'
                        }
						#tableheaderfont
						{
						 font-size:18px;
						 font-weight:bold;
						 background-color: #dedede;
                        }
                        #table2ndheaderfont
						{
						 font-size:14px;
						 font-weight:bold;
						 background-color: #dedede;
                        }
                        #table3rdheaderfont
						{
						 font-size:11px;
						 font-weight:bold;
						 background-color: #dedede;
                        }
                        #tableSubjectfont
						{
						 font-size:11px;
						 font-weight:bold;
						 background-color: #dedede;
                        }
		 		      #tableRemarksfont
						{
						 font-size:11px;
						 font-weight:bold;
						 background-color: #dedede;
		 		         text-align:left;
		 		         padding:10px 10px 10px 20px;
                        }
		 		      #tableExamfont
						{
						 font-size:14px;
						 font-weight:bold;
						 text-align:left;
		 		         padding-left:40px;
                        }
						</style>
						</head>";
   	 
   	 $this->config->load('config');
   	       $htmlbody="<body>";
   	       $htmlbody.="<div>
   	       <table align='center' style='text-align:center; padding-top:0px;padding-bottom:20px' border='0' width='100%'>
   	       <tr><td width='20%' style='text-align:right'><img src='assets/img/techb/logo.png' width='70' height='70'></td>
   	       <td width='40%' style='text-align:left' id='tablePageHeader'>".$_SESSION['institute_name']."</td>
   	       <td width='40%' id='tablePageHeader'  style='text-align:left;padding-left:80px;'>Attendence Report<br/></td>
   	       </tr>
   	       
   	       </table> </div>";
   	       
   	       $htmlbody.="<table align='center' class='gridtable'>
   	       <thead>
   	       <tr>
   	       <th class='header' width='5%'>#</th>
   	       <th class='yellow header headerSortDown' width='5%'>Official ID</th>
   	       <th class='green header' width='38%'>Name</th>
   	       <th class='green header' width='15%'>Department</th>
   	       <th class='red header' width='25%'>Date and Time</th>
   	       <th class='red header' width='10%'>Remarks</th>
   	       </tr>
   	       </thead>
   	       <tbody>";
   	       $employees=$data['employees'];
   	       $ind=1;
   	       foreach ($employees as $row)
   	       {
   	        $htmlbody.='<tr>
   	       		<td>'.$ind++.'</td>
   	       		<td>'.$row['EMP_ID'].'</td>
   	       		<td>'.$row['name'].'</td>
   	       		<td>'.$row['dept_id'].'</td>
   	       		<td>'.$row['date_time'].'</td>
   	       		<td>'.$row['in_out'].'</td>
   	       		</tr>';
   	       	       
   	       }
   	       $htmlbody.='</tbody>';
   	       $htmlbody.='</table>';
   	       
   	       $htmlFooter="</body></html>";
   	       $html= $htmlHeader.$htmlbody.$htmlFooter;
   	       
   	       $this->load->helper(array('dompdf', 'file'));
   	       //pdf_create($html, 'DetailResultCard',TRUE,'landscape');
   	       pdf_create($html, 'DetailResultCard',FALSE,'portrait','pdf/allCards.pdf');
   	       
   	       $return = array();
   	       $return['msg'] = '';
   	       $return['error'] = false;
   	       $return['msg']="pdf/allCards.pdf";
   	       echo  json_encode($return);
   	       /*
   	       header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
   	       header('Content-Disposition: attachment;filename="exam_reslut.xlsx"');
   	       header('Cache-Control: max-age=0');
   	        
   	       $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
   	       $objWriter->save('php://output');
   	       */
   	 
   }
  /////////////////////////////////////////////
}